import DatePicker from "components/antd-compatible/date-picker";
import { messages, queryCurrencyPrecision } from 'share/common';
import React from 'react';
import { Popover, Table, Select,  InputNumber, Input } from 'antd';

const showUserInfo = {
  Y: messages('expense-1.key184')/*完整*/ + '-' + messages('expense-1.key185')/*发票上显示了姓名和身份证号*/,
  N: messages('expense-1.key186')/*不完整*/ + '-' + messages('expense-1.key187')/*发票上姓名和身份证未全部显示*/
};

const showInternalInfo = {
  Y: messages('common.yes')/*是*/,
  N: messages('common.no')/*否*/
};

const cardsignTypeInfo = {
  APPCARDSIGN: messages('expense-1.key188')/*微信*/,
  JSCARDSIGN: messages('expense-1.key188')/*微信*/,
  WXFWCARDSIGN: messages('expense-1.key188')/*微信*/,
  ALICARDSIGN: messages('expense-1.key32')/*支付宝*/,
  HAND: messages('expense-1.key189')/*手录*/,
  OCR: messages('expense-1.key190')/*拍照识别*/,
  SCANNING: messages('expense-1.key191')/*扫二维码*/,
  MAIL: messages('expense-1.key192')/*邮箱导入*/
};

function TollType(props = {}) {
  return (
    <span>
      {props.value === '06' ? messages('expense-1.key75')/*存在*/ : messages('expense-1.key76')/*不存在*/}
  </span>
  );
}

const changeColumns = [
  {
    title: messages('expense-1.key708')/*修改日期*/,
    dataIndex: 'createdDate',
    render: (value) => new Date(value).format('yyyy-MM-dd hh:mm:ss')
  },
  {
    title: messages('expense-1.key709')/*修改内容*/,
    dataIndex: 'newValue',
    render: (value, record) => messages('expense-1.key100', {
      arg1: record.property,
      arg2: record.oldValue || messages('expense-1.key714')/*空*/,
      arg3: record.newValue || messages('expense-1.key714')/*空*/
    })/*{arg1}：从 {arg2} 改为 {arg3}*/
  },
  {
    title: messages('expense-1.key710')/*修改人员*/,
    dataIndex: 'operatorName',
    render: (value) => value || '-'
  },
];

function renderPopOver(value, changes) {
  if (changes && changes[0]) {
    return <Popover content={<Table pagination={false} rowKey={'createdDate'} size={'small'} columns={changeColumns} dataSource={changes}/>}>
      <span className="adjunct-item-cell-value">{value}</span>
    </Popover>;
  }
  return value;
}


const handleGoodsName = (invoiceGoods) => {
  let goodsNameArray = [];
  if (invoiceGoods && invoiceGoods.length > 0) {
    invoiceGoods.map(item => {
      let str;
      if (item.goodsName) {
        let goodsNameArray = item.goodsName.match(/\*(\S*)\*/);
        if (goodsNameArray && goodsNameArray.length > 1) {
          str = goodsNameArray[1];
        } else {
          str = item.goodsName;
        }
      }
      if (str) {
        goodsNameArray.push(str);
      } else {
        goodsNameArray.push(item.goodsName);
      }
    });
    let goodsNameNoRepeat = new Set(goodsNameArray);
    goodsNameArray = [...goodsNameNoRepeat];
  }
  return goodsNameArray.join(',') || '-';
};

const INVOICE_MAP = {
  headerTitle: {
    name: messages('expense-1.key711')/*发票名称*/,
    render: (value, record, changes) => renderPopOver(record.type, changes)
  },
  origin: {
    name: messages('expense-1.key93')/*发票来源*/,
    render: (value, record) => record.cardsignType
  },
  type: {
    name: messages('expense-1.key711')/*发票名称*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  taxRate: {
    name: messages('expense-1.key69')/*税率*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  vatInvoiceCurrencyCode: {
    name: messages('common.currency')/*币种*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  billingTime: {
    name: messages('expense-1.key77')/*开票日期*/,
    render: (value, record, changes) => value ? renderPopOver(new Date(value * 1000).format('yyyy-MM-dd'), changes) : '-'
  },
  billingCode: {
    name: messages('expense-1.key78')/*发票代码*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  billingNo: {
    name: messages('expense-1.key79')/*发票号码*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  deviceNumber: {
    name: messages('expense-1.key80')/*机器编号*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  checkCode: {
    name: messages('expense-1.key81')/*校验码*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  receiptOwner: {
    name: messages('expense-1.key82')/*归属人*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  idCardNo: {
    name: messages('expense-1.key83')/*身份证号*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  showUserInfo: {
    name: messages('expense-1.key84')/*人员信息*/,
    render: (value, record, changes) => value ? renderPopOver(showUserInfo[value], changes) : '-'
  },
  internalStaff: {
    name: messages('expense-1.key85')/*是否内部员工发票*/,
    render: (value, record, changes) => value ? renderPopOver(showInternalInfo[value], changes) : '-'
  },
  agentTag:{
    name: messages('expense-1.key174')/*是否代开*/,
    render: (value, record, changes) => value ? renderPopOver(showInternalInfo[value], changes) : '-'
  },
  domesticPassengers: {
    name: messages('expense-1.key86')/*国内旅客运输(非港澳台)*/,
    render: (value, record, changes) => value ? renderPopOver(showInternalInfo[value], changes) : '-'
  },
  tollType: {
    name: messages('expense-1.key92')/*通行费标志*/,
    render: (value, record, changes) => value ? <TollType value={value}/> : '-',
    isHide: (value, record) => record.invoiceTypeNo !== '14'
  },
  remark: {
    name: messages('common.remark')/*备注temp*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  cardsignType: {
    name: messages('expense-1.key93')/*发票来源*/,
    render: (value, record, changes) => value ? renderPopOver(cardsignTypeInfo[value], changes) : '-'
  },
  title: {
    name: messages('expense-1.key94')/*购买方*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-',
    truthy: (value, record) => record.suggestedTitle
  },
  draweeNo: {
    name: messages('expense-1.key95')/*纳税人识别号*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-',
    truthy: (value, record) => record.suggestedDraweeNo
  },
  draweeAddressPhone: {
    name: messages('expense-1.key96')/*地址/电话*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-',
    truthy: (value, record) => record.suggestedDraweeAddressPhone
  },
  draweeBankAccount: {
    name: messages('expense-1.key97')/*开户行/账户*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-',
    truthy: (value, record) => record.suggestedDraweeBankAccount
  },
  payee: {
    name: messages('expense-1.key98')/*销售方*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  goodsName:{
    name: messages('expense-1.key211')/*服务名称*/,
    render: (value, record, changes) => record.invoiceGoods &&  record.invoiceGoods[0] ? renderPopOver(handleGoodsName(record.invoiceGoods), changes) : '-'
  },
  payeeNo: {
    name: messages('expense-1.key95')/*纳税人识别号*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  payeeAddressPhone: {
    name: messages('expense-1.key96')/*地址/电话*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-'
  },
  payeeBankAccount: {
    name: messages('expense-1.key97')/*开户行/账户*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-',
  },
  nonDeductibleAmount: {
    name: messages('expense-1.key91')/*不可抵扣*/,
    render: (value, record, changes) => value ? renderPopOver(`${record.vatInvoiceCurrencyCode} ${(value / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`, changes) : '-',
    isHide: (value) => !value
  },
  feeWithoutTax: {
    name: messages('expense-1.key712')/*不含税金额*/,
    render: (value, record, changes) => typeof value === 'number' ? renderPopOver(`${record.vatInvoiceCurrencyCode} ${(value / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`, changes) : '-',
  },
  tax: {
    name: messages('expense-1.key90')/*税额合计*/,
    render: (value, record, changes) => value !== null ? renderPopOver(`${record.vatInvoiceCurrencyCode} ${(Number(value) / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`, changes) : '-',
  },
  taxString: {
    name: messages('expense-1.key90')/*税额合计*/,
    render: (value, record, changes) => value ? renderPopOver(`${record.vatInvoiceCurrencyCode} ${(Number(value) / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`, changes) : '-',
  },
  fee: {
    name: messages('expense-1.key87')/*价税合计*/,
    render: (value, record, changes) => typeof value === 'number' ? renderPopOver(`${record.vatInvoiceCurrencyCode} ${(value / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`, changes) : '-',
  },
  totalAmount: {
    name: messages('common.total.amount')/*总金额*/,
    render: (value, record) => renderPopOver(`${record.vatInvoiceCurrencyCode} ${((record.fee + (record.nonDeductibleAmount || 0)) / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`)
  },
  amount: {
    name: messages('expense-1.key88')/*金额合计*/,
    render: (value, record) => renderPopOver(`${((record.fee + (record.nonDeductibleAmount || 0)) / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`)
  },
  invoicedReceiptAmount: {
    name: messages('expense-1.key726')/*本次报销占用金额*/,
    render: (value, record) => renderPopOver(`${record.vatInvoiceCurrencyCode} ${((value === null ? record.unUsedAmount : value) / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`)
  },
  unUsedAmount: {
    name: messages('expense-1.key35')/*可报金额*/,
    render: (value, record) => renderPopOver(`${record.vatInvoiceCurrencyCode} ${((value) / 100).toFixed(queryCurrencyPrecision(record.vatInvoiceCurrencyCode))}`)
  },
  slicingAttachmentOID: {
    name: messages('expense-1.key164')/*发票附件*/,
    render: (value, record, changes) => value ? renderPopOver(value, changes) : '-',
  },
};

export default INVOICE_MAP;
